import request from '@/config/axios'

// 删除文件
export const deleteFile = (relativePath: string) => {
  return request.delete({
    url: `/admin-api/reading/book-file/delete?path=${encodeURIComponent(relativePath)}`
  })
}

// 上传绘本集封面（支持替换旧文件）
export const uploadBookSetCoverWithReplace = (bookSetId: number, file: File, oldCoverPath?: string) => {
  const formData = new FormData()
  formData.append('cover', file)
  if (oldCoverPath) {
    formData.append('oldCoverPath', oldCoverPath)
  }
  
  return request.post({
    url: `/admin-api/reading/book-file/book-set/${bookSetId}/cover`,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传绘本封面（支持替换旧文件）
export const uploadBookCoverWithReplace = (bookSetId: number, bookId: number, file: File, oldCoverPath?: string) => {
  const formData = new FormData()
  formData.append('cover', file)
  if (oldCoverPath) {
    formData.append('oldCoverPath', oldCoverPath)
  }
  
  return request.post({
    url: `/admin-api/reading/book-file/book-set/${bookSetId}/book/${bookId}/cover`,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传绘本页面图片（支持替换旧文件）
export const uploadBookPageWithReplace = (bookSetId: number, bookId: number, pageNumber: number, file: File, oldImagePath?: string) => {
  const formData = new FormData()
  formData.append('page', file)
  if (oldImagePath) {
    formData.append('oldImagePath', oldImagePath)
  }
  
  return request.post({
    url: `/admin-api/reading/book-file/book-set/${bookSetId}/book/${bookId}/page/${pageNumber}`,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传绘本音频（支持替换旧文件）
export const uploadBookAudioWithReplace = (bookSetId: number, bookId: number, pageNumber: number, file: File, oldAudioPath?: string) => {
  const formData = new FormData()
  formData.append('audio', file)
  if (oldAudioPath) {
    formData.append('oldAudioPath', oldAudioPath)
  }
  
  return request.post({
    url: `/admin-api/reading/book-file/book-set/${bookSetId}/book/${bookId}/audio?pageNumber=${pageNumber}`,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
